Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs
Imports Vintasoft.Imaging

Namespace Imaging
	Public Partial Class RenderingSettingsForm
		Inherits Form

		#Region "Constructor"

		Public Sub New(renderingSettings As RenderingSettings)
			InitializeComponent()

			_renderingSettings = renderingSettings

			interpolationModeComboBox.Items.Add(InterpolationMode.Bicubic)
			interpolationModeComboBox.Items.Add(InterpolationMode.Bilinear)
			interpolationModeComboBox.Items.Add(InterpolationMode.[Default])
			interpolationModeComboBox.Items.Add(InterpolationMode.High)
			interpolationModeComboBox.Items.Add(InterpolationMode.HighQualityBicubic)
			interpolationModeComboBox.Items.Add(InterpolationMode.HighQualityBilinear)
			interpolationModeComboBox.Items.Add(InterpolationMode.Low)
			interpolationModeComboBox.Items.Add(InterpolationMode.NearestNeighbor)

			smoothingModeComboBox.Items.Add(SmoothingMode.AntiAlias)
			smoothingModeComboBox.Items.Add(SmoothingMode.[Default])
			smoothingModeComboBox.Items.Add(SmoothingMode.HighQuality)
			smoothingModeComboBox.Items.Add(SmoothingMode.HighSpeed)
			smoothingModeComboBox.Items.Add(SmoothingMode.None)

			If renderingSettings.IsEmpty OrElse renderingSettings.Resolution.IsEmpty() Then
				cbDefault.Checked = True
				smoothingModeComboBox.SelectedItem = SmoothingMode.HighQuality
				interpolationModeComboBox.SelectedItem = InterpolationMode.HighQualityBilinear
			Else
				cbDefault.Checked = False
				smoothingModeComboBox.SelectedItem = renderingSettings.SmoothingMode
				interpolationModeComboBox.SelectedItem = renderingSettings.InterpolationMode
				horizontalResolution.Value = CInt(renderingSettings.Resolution.Horizontal)
				verticalResolution.Value = CInt(renderingSettings.Resolution.Vertical)

			End If
		End Sub

		#End Region


		#Region "Properties"

		Private _renderingSettings As RenderingSettings
		Public ReadOnly Property RenderingSettings() As RenderingSettings
			Get
				Return _renderingSettings
			End Get
		End Property

		#End Region


		#Region "Methods"

		Public Shared Sub SetRenderingSettingsIfNeed(images As ImageCollection, encoder As EncoderBase, defaultRenderingSettings As RenderingSettings)
			If encoder Is Nothing OrElse Not (TypeOf encoder Is IPdfEncoder) Then
				For i As Integer = 0 To images.Count - 1
					If images(i).SourceInfo.DecoderName = "Pdf" Then
						Dim settingsForm As New RenderingSettingsForm(defaultRenderingSettings.CreateClone())
						If settingsForm.ShowDialog() = DialogResult.OK Then
							images.SetRenderingSettings(settingsForm.RenderingSettings)
						Else
							Return
						End If
						Exit For
					End If
				Next
			End If
		End Sub

		Public Shared Sub SetRenderingSettingsIfNeed(image As VintasoftImage, encoder As EncoderBase, defaultRenderingSettings As RenderingSettings)
			If encoder Is Nothing OrElse Not (TypeOf encoder Is IPdfEncoder) Then
				If image.SourceInfo.DecoderName = "Pdf" Then
					Dim settingsForm As New RenderingSettingsForm(defaultRenderingSettings.CreateClone())
					If settingsForm.ShowDialog() = DialogResult.OK Then
						image.RenderingSettings = settingsForm.RenderingSettings
					End If
				End If
			End If
		End Sub

		Private Sub btOk_Click(sender As Object, e As EventArgs)
			If cbDefault.Checked Then
				_renderingSettings = Vintasoft.Imaging.Codecs.RenderingSettings.Empty
			Else
				_renderingSettings = New RenderingSettings(CSng(horizontalResolution.Value), CSng(verticalResolution.Value), CType(interpolationModeComboBox.SelectedItem, InterpolationMode), CType(smoothingModeComboBox.SelectedItem, SmoothingMode))
			End If
			DialogResult = System.Windows.Forms.DialogResult.OK
			Close()
		End Sub

		Private Sub cbDefault_CheckedChanged(sender As Object, e As EventArgs)
			gbCustomSettings.Enabled = Not cbDefault.Checked
		End Sub

		Private Sub btCancel_Click(sender As Object, e As EventArgs)
			DialogResult = System.Windows.Forms.DialogResult.Abort
			Close()
		End Sub

		#End Region

	End Class
End Namespace
